<script setup>
import { ref, reactive, onMounted, computed } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useCurrentWatchStore, useIuserInfoStore } from '@T2/store/users.ts';
import { deepCopy } from '@T2/utils/utils.ts';
import { getToSomeProjectPath } from '@T2/utils/users.ts';
import { warnApi, iuserApi, projectById } from '@T2/service/api.ts';
import dayjs from 'dayjs';

const router = useRouter();
const currentWatchStore = useCurrentWatchStore();
const projectList = deepCopy(currentWatchStore.getProjectsOfValidMenus);
const iuserInfoStore = useIuserInfoStore();
const { info, getHandleRight } = iuserInfoStore;
const RIGHT = getHandleRight(['345']);

const nums = computed(() => {
  let total = 0, isIng = 0, end = 0;
  projectList.forEach(i => {
    total++;
    if (i.status === '已开通') {
      isIng++;
    } else if (i.status === '到期') {
      end++
    }
  });
  return { total, isIng, end };
});

onMounted(() => {
  getWarnList();
});

const warns = ref([]);
const getWarnList = () => {
  warnApi({
    method: 'get',
    params: {
      reset_time__isnull: true,
      confirm_user__isnull: true,
    }
  }).then(res => {
    warns.value = res?.results || [];
  })
}

const goToProject = (project) => {
  const path = getToSomeProjectPath(project.id);
  if (path) {
    router.push(path);
  } else {
    ElMessage.error('暂无登入权限，请联系管理员');
  }
}

const adminRt = reactive({
  visible: false,
  iusers: [],
  model: {},
  name: '',
});
const adminFilter = reactive({
  page: 1,
  // pagesize: 20,
  ordering: 'create_time',
  search: '',
});
const adminFormRef = ref(null);
const handleAdmin = (isAble = false, project) => {
  if (!isAble) {
    return;
  }
  getRoles();
  adminRt.visible = true;
  adminRt.model = {
    id: project.id,
    admin_user: project.admin_user,
  };
  adminRt.name = project.name;
}
const getRoles = () => {
  iuserApi({
    method: 'get',
    params: {
      ...adminFilter,
    }
  }).then(res => {
    adminRt.iusers = res?.results || [];
  });
}
const handleAdminSubmit = () => {
  adminRt.visible = false;
  const userObj = adminRt.iusers.find(i => i.id === adminRt.model.admin_user);
  projectById({
    method: 'patch',
    id: adminRt.model.id,
    data: {
      admin_user: adminRt.model.admin_user || null,
    }
  }).then(res => {
    ElMessage.success('设置成功！');
    currentWatchStore.setProjectAdmin(adminRt.model.id, adminRt.model.admin_user, userObj?.truename);
    window.location.reload();
  });
}
</script>

<template>
  <div class="view">
    <div class="grid-item1 card flex-column">
      <label class="page-name">工作台</label>
      <div class="flex">
        <div class="avator-icon flex-center-center">{{ info.truename.substr(-2) }}</div>
        <div class="hello-info flex-column flex-center">
          <p>Hi，{{ info.truename }}，有事做有所期待，日子就是幸福的！</p>
          <span>{{ dayjs().format('YYYY-MM-DD dddd') }}</span>
        </div>
        
        <div class="nums-box flex--center first">
          <img :src="require('@T2/assets/workbench/projects-nums.png')" />
          <div class="flex-column flex-center">
            <label>项目总数</label>
            <strong>{{ nums.total }}</strong>
          </div>
        </div>
        
        <div class="nums-box flex--center">
          <img :src="require('@T2/assets/workbench/projects-ing.png')" />
          <div class="flex-column flex-center">
            <label>正常运行</label>
            <strong>{{ nums.isIng }}</strong>
          </div>
        </div>
        
        <div class="nums-box flex--center">
          <img :src="require('@T2/assets/workbench/projects-end.png')" />
          <div class="flex-column flex-center">
            <label>停用</label>
            <strong>{{ nums.end }}</strong>
          </div>
        </div>
        
        <div class="nums-box flex--center">
          <img :src="require('@T2/assets/workbench/projects-offline.png')" />
          <div class="flex-column flex-center">
            <label>离线</label>
            <strong>0</strong>
          </div>
        </div>
      </div>
    </div>
    
    <div class="grid-item2 card flex-column">
      <div class="project-header flex-sb">
        <label class="header">项目列表</label>
      </div>
      <div class="projects-box scroller-aside">
        <div class="project-item" v-for="item in projectList" @click="goToProject(item)">
          <div class="run-days">运行天数：{{ item.run_days }}</div>
          <label class="project-name">{{ item.name }}</label>
          <div class="project_admin flex--center" v-if="item.admin_user_truename">
            <label>项目管理员：</label>
            <span>{{ item.admin_user_truename}}</span>
            <!-- <el-button link type="primary" @click.stop="handleAdmin(RIGHT['345'], item)" :disabled="!RIGHT['345']">
              <el-icon size="20" style="margin-left: 10px;">
                <Edit />
              </el-icon>
            </el-button> -->
          </div>
          <div class="warn-box flex-column flex-center-center" :class="{'iswarn': item.warn_number }">
            <strong>{{ item.warn_number}}</strong>
            <label>报警数</label>
          </div>
        </div>
      </div>
    </div>
    
    <div class="grid-item3 card flex-column">
      <div class="message-header flex-sb">
        <label class="header">消息通知</label>
        <!-- <span>查看更多</span>
        <el-icon></el-icon> -->
      </div>
      <template v-if="warns.length">
        <div class="message-item warns flex-column" v-for="item in warns">
          <div class="title-line flex">
            <span>{{ item.project_name }}</span>&nbsp;
            <span v-if="item.unit">/ {{ item.unit_name }}</span>&nbsp;
            <span v-if="item.device">/ {{ item.device_name }}</span>&nbsp;
            <span>| {{ item.create_time }}</span>
          </div>
          <strong>{{ item.info }}</strong>
        </div>
      </template>
      <template v-else>
        <div class="nomessage flex-center">暂无消息</div>
      </template>
    </div>
  </div>
  
  <el-dialog width="500px" title="项目管理员设置" v-model="adminRt.visible">
    <el-form
      ref="adminFormRef"
      :model="adminRt.model"
      label-width="120px"
    >
      <el-form-item label="项目名称：">
        {{ adminRt.name }}
      </el-form-item>
      
      <el-form-item prop="admin_user" label="设置管理员：">
        <el-select v-model="adminRt.model.admin_user" clearable>
          <el-option
            v-for="item in adminRt.iusers"
            :value="item.id"
            :key="item.id"
            :label="item.truename"
          />
        </el-select>
      </el-form-item>
      
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="adminRt.visible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdminSubmit()">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
.view {
  width: 100%;
  height: 100%;
  padding: 24px;
  display: grid;
  gap: 16px;
  overflow-x: hidden;
  overflow-y: overlay;
  grid-template-columns: 1fr minmax(328px, 33%);
  grid-template-rows: 186px 1fr;
}
.card {
  background: #FFFFFF;
  border-radius: 4px;
  overflow: hidden;
}
.grid-item1 {
  grid-column: span 2;
  padding: 24px;
  .page-name {
    font-weight: 600;
    font-size: 26px;
    color: #000000;
    height: 28px;
    line-height: 28px;
    margin-bottom: 40px;
  }
  .avator-icon {
    width: 70px;
    height: 70px;
    border-radius: 50%;
    font-size: 20px;
    background-color: rgba(170, 170, 255, 0.5);
    flex-shrink: 0;
  }
  .hello-info {
    margin-left: 32px;
    p {
      font-weight: 500;
      font-size: 20px;
      color: #333333;
      // height: 23px;
      line-height: 23px;
      margin-bottom: 8px;
    }
    span {
      font-weight: 400;
      font-size: 16px;
      color: #666666;
      height: 22px;
      line-height: 22px;
    }
  }
  .first {
    margin-left: auto;
  }
  .nums-box {
    width: 245px;
    height: 70px;
    img {
      width: 36px;
      height: 36px;
      margin-right: 26px;
    }
    label {
      font-weight: 400;
      font-size: 14px;
      color: #333333;
      height: 22px;
      line-height: 22px;
    }
    strong {
      font-weight: 600;
      font-size: 26px;
      color: #111111;
      height: 45px;
      line-height: 45px;
    }
  }
}
label.header {
  font-weight: 600;
  font-size: 18px;
  color: #111111;
  height: 28px;
  line-height: 28px;
  position: relative;
  padding-left: 12px;
  &::before {
    content: '';
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 4px;
    height: 18px;
    background-color: #0079FE;
  }
}
.grid-item2 {
  padding: 24px;
  .project-header {
    margin-bottom: 32px;
  }
  .projects-box {
    flex: 1;
    overflow: overlay;
    overflow-x: hidden;
    display: grid;
    gap: 16px;
    grid-template-columns: 1fr 1fr;
  }
  .project-item {
    padding: 48px 24px;
    height: 210px;
    background: #F7FAFF;
    border-radius: 4px;
    position: relative;
    cursor: pointer;
    .run-days {
      position: absolute;
      top: 0;
      right: 0;
      width: 126px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      background: #E5F1FF;
      border-radius: 0px 4px;
      font-weight: 500;
      font-size: 16px;
      color: #333333;
    }
    .warn-box {
      position: absolute;
      right: 24px;
      bottom: 36px;
      width: 70px;
      height: 70px;
      border-radius: 50%;
      border: 3px solid #B1D5FF;
      background-color: #0C7BFF;
      color: #fff;
      line-height: 28px;
      &.iswarn {
        border: 3px solid #FFB1B2;
        background-color: #F50003;
      }
      strong {
        font-weight: 500;
        font-size: 24px;
        height: 28px;
      }
      label {
        font-weight: 500;
        font-size: 12px;
        height: 28px;
      }
    }
    label.project-name {
      font-weight: 500;
      font-size: 26px;
      color: #333333;
      height: 28px;
      line-height: 28px;
    }
    .project_admin {
      margin-top: 10px;
    }
  }
}
.grid-item3 {
  padding: 12px;
  .message-header {
    margin-bottom: 22px;
  }
  .nomessage {
    width: 100%;
  }
  .message-item {
    padding: 12px;
    line-height: 28px;
    &:hover {
      background-color: #F7FAFF;
      cursor: pointer;
    }
    .title-line {
      font-weight: 500;
      font-size: 12px;
      color: #999999;
      height: 28px;
      span {
        white-space: nowrap;
      }
    }
    strong {
      font-weight: 500;
      font-size: 14px;
      height: 28px;
      color: #333333;
    }
  }
}
</style>